﻿package net.libra.core
{
	import net.libra.loader.AMFLoader;
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	
	import net.libra.utils.ObjectUtil;

	/**
	 * 远程服务对象。对象用于调用远程接口。
	 * @author DanielLu
	 */
	public class Service
	{
		// 网关地址
		public static var GATEWAY : String = "";
		// public static var GATEWAY : String = "http://localhost/bzcq/game/GameServices/flashservices/gateway.php";
		private static var loader : AMFLoader = new AMFLoader ();
		protected var onResult : Function;
		protected var onFault : Function;
		protected var loader : AMFLoader;

		/**
		 * 创建一个服务委托
		 * @param onResult		调用成功的回调方法
		 * @param onFault		调用失败的回调方法
		 */
		public function Service ( onResult : Function = null , onFault : Function = null )
		{
			this.onResult = onResult;
			this.onFault = onFault;
			//
			if (Service.loader.isLoading ())
			{
				this.loader = new AMFLoader ();
			}
			else
			{
				this.loader = Service.loader;
			}
			this.loader.setGateway ( GATEWAY );
			this.loader.addCompleteListener ( this.__onResult );
			this.loader.addFailureListener ( this.__onFault );
		}

		private function __onResult ( e : Event ) : void
		{
			//print log
			printReceivedLog(this.loader.getMethodName(),this.loader.getData());
			
			this.loader.removeCompleteListener ( this.__onResult );
			this.loader.removeFailureListener ( this.__onFault );
			if (this.onResult != null)
			{
				this.onResult ( this.loader.getData () );
			}
			this.onResult = null;
			this.onFault = null;
		}

		private function __onFault ( e : Event ) : void
		{
			this.loader.removeCompleteListener ( this.__onResult );
			this.loader.removeFailureListener ( this.__onFault );
			
			if (this.onFault != null)
			{
				if (this.loader.getData () is NetStatusEvent)
				{
					try
					{
						this.onFault ( "[NetStatusError：" + this.loader.getData ().info.code + "]" );
						
					}
					catch(e : Error)
					{
						this.onFault ( this.loader.getData () );
					}
				}
				else
				{
					this.onFault ( this.loader.getData () );
					
				}
			}
			this.onResult = null;
			this.onFault = null;
		}

		/**
		 * 链接远程对象
		 */
		public function doLoad( methodName : String , ...param ) : void
		{
			//print log
//			trace("param.length="+param.length);
			printSendLog(methodName,param);
			
			this.loader.setMethodName ( methodName );
			this.loader.setArgs.apply ( null , param );
			this.loader.doLoad ();
			;
		}
		/**
		 * 链接远程对象
		 */
		public function doLoad2( methodName : String , ...param ) : void
		{
			//print log
//			trace("param.length="+param.length);
			printSendLog(methodName,param);
			
			this.loader.setMethodName ( methodName );
			this.loader.setArgs.apply ( null , param );
			this.loader.doLoad2 ();
		}
		
		/**
		 * JSON形式请求数据，测试用
		 */
		public function doLoad3( methodName : String , ...param ) : void
		{
			printSendLog(methodName,param);
			
			this.loader.setMethodName ( methodName );
			this.loader.setArgs.apply ( null , param );
			this.loader.doLoad3 ();
		}
		
		public function doLoadLog( logFile : String, methodName : String , ...param ) : void
		{
			//print log
			//			trace("param.length="+param.length);
			printSendLog(methodName,param);
			
			this.loader.setMethodName ( methodName );
			this.loader.setArgs.apply ( null , param );
			this.loader.doLoadLog(logFile);
		}

		public function printSendLog(methodName:String,param:Array):void
		{
			//send log
			var log:String = ">>>[method:" + methodName;
			log +="||param:(";
//			log +="["+param.length+"]||param:(";
			for(var i:int = 0;i<param.length;i++)
			{
				if(i!=0) log+=",";
				log += "'"+param[i]+"'";
			}
			log += ")]";
			Log.info(log);
		}
		public function printReceivedLog(methodName:String,data:Object):void
		{
			//received log
			var log:String = "<<<[method:" + methodName;
			log += "||data:" + "\n";
			if(data is String)
			{
				log += String(data);
			}else if(data is Object){
				log += ObjectUtil.deepObjectToString(data);
			}
//			for each(var i:* in data)
//			{
//				log += i+":"+data[i] + "";
//			}
			log += "]";
			Log.info(log);
		}
	}
}
